const router = require("koa-router")();
const result = require("../../config/result");
const {
  getToken,
  addUrl,
  whereUrl,
  updateUrl,
} = require("../../config/databaseapi");
const {
  regCheck,
  shopInfor,
  categoryCheck,
  unitCheck,
  dishCheck,
} = require("../../config/checking");
const { Auth } = require("../../token/auth");
const { upload, cosfun } = require("../../cos/cos");
const moment = require("moment");
moment.locale("zh-cn");

//获取菜品单位接口
router.get("/obtainunit", new Auth().m, async (ctx) => {
  const query = `db.collection('dishunit').get()`;
  try {
    const res = await new getToken().posteve(whereUrl, query);
    const data = res.data.map((item) => {
      return JSON.parse(item);
    });
    new result(ctx, "SUCCESS", 200, data).answer();
  } catch (error) {
    new result(ctx, "FAIL", 500).answer();
  }
});
//添加自定义单位接口
router.post("/dishunit", new Auth().m, async (ctx) => {
  const { unit } = ctx.request.body;
  new unitCheck(ctx.unit).start();
  const unid = new Date().getTime();
  //查询单位是否存在
  const whereQuery = `db.collection('dishunit').where({label:'${unit}'}).get()`;
  const query = `db.collection('dishunit').add({data:{value:'${unit}',label:'${unit}',unid:'${unid}'}})`;
  try {
    const res = await new getToken().posteve(whereUrl, whereQuery);
    if (res.data.length > 0) {
      new result(ctx, "已有单位", 202).answer();
    } else {
      await new getToken().posteve(addUrl, query);
      new result(ctx, "添加成功", 200).answer();
    }
  } catch (error) {
    new result(ctx, "添加失败", 500).answer();
  }
});
//上架菜品接口
router.post("/uploaddishes", new Auth().m, async (ctx) => {
  const { name, category, id, unitprice, unit, image, value } =
    ctx.request.body;
  new dishCheck(ctx, name, category, unitprice, unit, image, value).start();
  let time = moment().utcOffset(8).format("YYYY-MM-DD HH:mm:ss");
  const query = `db.collection('dish').add({data:{
        category:'${category}',
        name:'${name}',
        unitprice:${unitprice},
        unit:'${unit}',
        image:${image},
        quantity:0,
        onsale:true,
        cid:'${value}',
        time:'${time}',
        monthlysale:0
    }})`;
  const count = `db.collection('category').where({cid:'${value}'}).update({data:{count:db.command.inc(1)}})`;
  try {
    await new getToken().posteve(addUrl, query);
    await new getToken().posteve(updateUrl, count);
    new result(ctx, "提交成功", 200).answer();
  } catch (error) {
    new result(ctx, "Fila", 500).answer();
  }
});
//获取菜品数据接口
router.get("/obtaindishes", new Auth().m, async (ctx) => {
  const { page } = ctx.query;
  let sk = page * 10;
  let query = `db.collection('dish').orderBy('time','desc').limit(10).skip(${sk}).get()`;
  try {
    const res = await new getToken().posteve(whereUrl, query);
    const data = res.data.map((item) => {
      return JSON.parse(item);
    });
    let tatal = { tatal: res.pager.Total };
    const arr = { ...{ result: data }, ...tatal };
    new result(ctx, "SUCCESS", 200, arr).answer();
  } catch (error) {
    new result(ctx, "FAIL", 500).answer();
  }
});
//下架菜品接口
router.get("/fromsale", new Auth().m, async (ctx) => {
  const { id, value } = ctx.query;
  const query = `db.collection('dish').doc('${id}').update({data:{onsale:false}})`;
  const dec = `db.collection('category').where({cid:'${value}'}).update({data:{count:db.command.inc(-1)}})`;
  try {
    new getToken().posteve(updateUrl, query);
    new getToken().posteve(updateUrl, dec);
    new result(ctx, "下架成功", 200).answer();
  } catch (error) {
    new result(ctx, "下架失败", 500).answer();
  }
});
//编辑菜品接口
router.post("/modifydishes", new Auth().m, async (ctx) => {
  const { name, category, id, unitprice, unit, image, value } =
    ctx.request.body;
  new dishCheck(ctx, name, category, unitprice, unit, image, value).start();
  let time = moment().utcOffset(8).format("YYYY-MM-DD HH:mm:ss");
  const query = `db.collection('dish').doc('${id}').update({data:{
        category:'${category}',
        name:'${name}',
        unitprice:${unitprice},
        unit:'${unit}',
        image:${image},
        quantity:0,
        onsale:true,
        cid:'${value}',
        time:'${time}'
    }})`;
  try {
    await new getToken().posteve(updateUrl, query);
    new result(ctx, "修改成功", 200).answer();
  } catch (error) {
    new result(ctx, "服务器错误", 500).answer();
  }
});
module.exports = router.routes();
